﻿package com.saumya.components 
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import com.saumya.components.calenderClips.DateBtn;
	import com.saumya.components.calenderClips.DatePickerBtn;
	
	import flash.events.MouseEvent;
	
	/**
	 * 
	 * @author Saumya
	 * @version 0.2
	 */
	public class SamCalender extends MovieClip
	{
		//declaring the visual elements
		public var resultClip:DatePickerBtn;
		//
		public var resultDate:TextField;
		public var currentMonth:TextField;
		public var currentYear:TextField;
		//
		public var backYearBtn:Sprite;
		public var nextYearBtn:Sprite;
		//
		public var backMonthBtn:Sprite;
		public var nextMonthBtn:Sprite;
		//
		public var closeDatePickerBtn:Sprite;
		//
		public var dtBox_1_mc:DateBtn;
		public var dtBox_2_mc:DateBtn;
		public var dtBox_3_mc:DateBtn;
		public var dtBox_4_mc:DateBtn;
		public var dtBox_5_mc:DateBtn;
		public var dtBox_6_mc:DateBtn;
		public var dtBox_7_mc:DateBtn;
		public var dtBox_8_mc:DateBtn;
		public var dtBox_9_mc:DateBtn;
		public var dtBox_10_mc:DateBtn;
		public var dtBox_11_mc:DateBtn;
		public var dtBox_12_mc:DateBtn;
		public var dtBox_13_mc:DateBtn;
		public var dtBox_14_mc:DateBtn;
		public var dtBox_15_mc:DateBtn;
		public var dtBox_16_mc:DateBtn;
		public var dtBox_17_mc:DateBtn;
		public var dtBox_18_mc:DateBtn;
		public var dtBox_19_mc:DateBtn;
		public var dtBox_20_mc:DateBtn;
		public var dtBox_21_mc:DateBtn;
		public var dtBox_22_mc:DateBtn;
		public var dtBox_23_mc:DateBtn;
		public var dtBox_24_mc:DateBtn;
		public var dtBox_25_mc:DateBtn;
		public var dtBox_26_mc:DateBtn;
		public var dtBox_27_mc:DateBtn;
		public var dtBox_28_mc:DateBtn;
		public var dtBox_29_mc:DateBtn;
		public var dtBox_30_mc:DateBtn;
		public var dtBox_31_mc:DateBtn;
		//The date variables
		private var userDate:Date = new Date();
		private var currentDate:Date = new Date();
		//
		private var userSelectedYear:uint = 2000;
		private var userSelectedMonth:uint = 1;
		private var userSelectedDay:uint = 1;
		
		/**
		 * Constructor
		 */
		public function SamCalender() 
		{
			trace(this, ' : Constructor : ');
			this.init();
		}
		
		/**
		 * Actual initialisation of the component
		 */
		private function init():void
		{
			trace(this, ' : init : ');
			this.showTheChooser(false);
			this.showTheUserBtn(true);
			//adds the handlers
			this.resultClip.addEventListener(MouseEvent.CLICK, onUserClickResultClip);
			this.closeDatePickerBtn.addEventListener(MouseEvent.CLICK, onClosePicker);
			//
			this.backMonthBtn.addEventListener(MouseEvent.CLICK, onBackMonthClick);
			this.backYearBtn.addEventListener(MouseEvent.CLICK, onBackYearClick);
			this.nextMonthBtn.addEventListener(MouseEvent.CLICK, onNextMonthClick);
			this.nextYearBtn.addEventListener(MouseEvent.CLICK, onNextYearClick);
			//
			this.addEventListener(DateBtn.DATE_SELETED_EVENT,onDateSelected);
			
			//initialisese the date
			this.initateDateLogic();
		}
		
		private function onDateSelected(e:Event):void 
		{
			//trace(this, ' : onDateSelected : ', e);
			var dtBtn:DateBtn = DateBtn(e.target);
			//trace(this, ' : onDateSelected : dtBtn=', dtBtn, ' : date=', dtBtn.getDate());
			var selectedDate:uint = dtBtn.getDate();
			this.userDate.date = selectedDate;
			//update the ui
			this.resultClip.setValueOfDate(this.userDate.toDateString());
			this.resultDate.text = this.userDate.toDateString();
			//hilite the seleted btn
			this.unHiliteDateButtons();//TODO: fix this
			dtBtn.hilite(true);
		}
		
		private function onNextYearClick(e:MouseEvent):void 
		{
			//trace('WCalender : onNextYearClick : ');
			//var nextYear:Date = new Date(this.currentDate.getFullYear);
			var nextYear:uint = this.userDate.fullYear + 1;
			this.userDate.fullYear = nextYear;
			//trace('WCalender : onNextYearClick : nextYear=', this.userDate);
			//
			this.updateDateUI();
		}
		
		private function onNextMonthClick(e:MouseEvent):void 
		{
			trace('WCalender : onNextMonthClick : ');
			if (this.userDate.month<=11)
			{
				var nextMonth:uint = this.userDate.month + 1;
			}else {
				//Do nothing
			}
			this.userDate.month = nextMonth;
			this.updateDateUI();
		}
		
		private function onBackYearClick(e:MouseEvent):void 
		{
			trace('WCalender : onBackYearClick : ');
			var lastYear:uint = this.userDate.fullYear - 1;
			this.userDate.fullYear = lastYear;
			this.updateDateUI();
		}
		
		private function onBackMonthClick(e:MouseEvent):void 
		{
			trace('WCalender : onBackMonthClick : ');
			if (this.userDate.month>1)
			{
				var lastMonth:uint = this.userDate.month - 1;
			}else {
				//Do nothing
			}
			this.userDate.month = lastMonth;
			this.updateDateUI();
		}
		
		
		/**
		 * Updates the date on the UI,wherever the date is visible
		 */
		private function updateDateUI():void
		{
			this.currentMonth.text = this.monthValue(this.userDate.getMonth());
			this.currentYear.text = this.userDate.getFullYear().toString();
			this.resultDate.text = this.userDate.toDateString();
			this.resultClip.setValueOfDate(this.userDate.toDateString());
			//
			this.unHiliteDateButtons();
			//
			this.updateDateChooser();
		}
		
		private function onClosePicker(e:MouseEvent):void 
		{
			this.showTheChooser(false);
			this.showTheUserBtn(true);
		}
		
		private function onUserClickResultClip(e:MouseEvent):void 
		{
			trace(this, ' : onUserClickResultClip : ');
			this.showTheChooser(true);
			this.showTheUserBtn(false);
		}
		
		/**
		 * Shows or hides the date choosing buttons
		 */
		private function showTheChooser(shouldShow:Boolean=true):void
		{
			this.resultDate.visible = shouldShow;
			this.currentMonth.visible = shouldShow;
			this.currentYear.visible = shouldShow;
			//
			this.backMonthBtn.visible = shouldShow;
			this.nextMonthBtn.visible = shouldShow;
			this.backYearBtn.visible = shouldShow;
			this.nextYearBtn.visible = shouldShow;
			//
			for (var i:uint = 1; i < 32;i++ )
			{
				var name:String = 'dtBox_' + i + '_mc';
				this[name].visible = shouldShow;
				
			}	
			//
			this.closeDatePickerBtn.visible = shouldShow;
		}
		
		/**
		 * Shows/hides the nice little button to display the date chooser
		 */
		private function showTheUserBtn(shouldShow:Boolean = true):void
		{
			this.resultClip.visible = shouldShow;
		}
		
		/**
		 * Displays current date
		 */
		private function showCurrentDate():void
		{
			this.resultClip.setValueOfDate(this.userDate.toDateString());
			this.resultDate.text = this.userDate.toDateString();
			this.currentMonth.text = this.monthValue(this.userDate.getMonth());
			this.currentYear.text = this.userDate.getFullYear().toString();
			//
			trace(this, ' : showCurrentDate : days=', this.daysInCurrentMonth(this.userDate).toString());
			//
			var numDaysInMonth:uint = this.daysInCurrentMonth(this.userDate);
			for (var i:uint = 0; i < numDaysInMonth ;i++ )
			{
				//trace(this, ' : showCurrentDate : day=', i);
				var name:String = 'dtBox_' + (i+1) + '_mc';
				//DateBtn(this[name]).visible = true;
				//hilite today
				var today:uint = this.currentDate.getDate();
				if (today==(i+1))
				{
					DateBtn(this[name]).hilite(true);
				}
				//sets the value of the date
				DateBtn(this[name]).setDate(i+1);
			}
		}
		
		/**
		 * Update the date chooser as per user selcted Year and Month
		 */
		private function updateDateChooser():void
		{
			
			trace(this, ' : updateDateChooser : days=', this.daysInCurrentMonth(this.userDate).toString());
			//
			var numDaysInMonth:uint = this.daysInCurrentMonth(this.userDate);
			for (var j:uint = 0; j < 31; j++ ) 
			{
				var name:String = 'dtBox_' + (j + 1) + '_mc';
				for (var i:uint = 0; i < numDaysInMonth ; i++ )
				{
					if (j<=i)
					{
						DateBtn(this[name]).visible = true;
						//sets the value of the date
						DateBtn(this[name]).setDate(i+1);
					}else {
						DateBtn(this[name]).visible = false;
					}
				}
				//sets the value of the date
				DateBtn(this[name]).setDate(j + 1);
				//hilites today
				//this day
				var today:uint = this.currentDate.getDate();
				var thisMonth:uint = this.currentDate.getMonth();
				var thisYear:uint = this.currentDate.getFullYear();
				//user selected day
				var userdate:uint = this.userDate.getDate();
				var usermonth:uint = this.userDate.getMonth();
				var userYear:uint = this.userDate.getFullYear();
				
				if (today==(j+1))
				if ((today==userdate)&&(thisMonth==usermonth)&&(thisYear==userYear))
				{
					DateBtn(this[name]).hilite(true);
				}else {
					DateBtn(this[name]).hilite(false);
				}
			}
		}
		
		
		/**
		 * Initiates the Date Loginc of the component
		 */
		private function initateDateLogic():void
		{
			trace(this, ' : initateDateLogic : ');
			trace(this, ' : current Date : ', this.currentDate.toDateString());
			trace(this, ' : user Date : ', this.userDate.toDateString());
			//displays the current date
			this.showCurrentDate();
		}
		
		private function unHiliteDateButtons():void
		{
			var numDaysInMonth:uint = this.daysInCurrentMonth(this.userDate);
			for (var i:uint = 0; i < numDaysInMonth ;i++ )
			{
				var name:String = 'dtBox_' + (i + 1) + '_mc';
				DateBtn(this[name]).hilite(false);
			}
		}
		
		
		/////////////////////////////////////////UTILITY METHODS//////////////////////////////////////////
		/**
		 * 
		 */
		private function monthValue(month:uint):String
		{
			var mValue:String = 'Jan';
			switch(month)
			{
				case 0 : mValue = 'Jan';
				break;
				case 1 : mValue = 'Feb';
				break;
				case 2 : mValue = 'March';
				break;
				case 3 : mValue = 'April';
				break;
				case 4 : mValue = 'May';
				break;
				case 5 : mValue = 'June';
				break;
				case 6 : mValue = 'July';
				break;
				case 7 : mValue = 'Aug';
				break;
				case 8 : mValue = 'Sep';
				break;
				case 9 : mValue = 'Oct';
				break;
				case 10 : mValue = 'Nov';
				break;
				case 11 : mValue = 'Dec';
				break;
				default : mValue = 'Invalid';
				break;
			}
			
			return mValue;
		}
		
		/**
		 * REturns the number of days in provided argument month
		 */
		private function daysInCurrentMonth(day:Date):uint
		{
			var noOfDays:uint = 30;
			//
			trace(this, ' : daysInCurrentMonth : date=', day );
			var monthNum:uint = day.getMonth();
			var yearNum:uint = day.getFullYear();
			var  isLeapYear:Boolean = false;
			if (yearNum%4==0)
			{
				isLeapYear = true;
			}else {
				isLeapYear = false;
			}
			switch(monthNum)
			{
				case 0 : noOfDays = 31;
				break;
				case 1 :
					if (isLeapYear)
					{
						noOfDays = 29;
					}else {
						noOfDays = 28;
					}
				break;
				case 2 : noOfDays = 31;
				break;
				case 3 : noOfDays = 30;
				break;
				case 4 : noOfDays = 31;
				break;
				case 5 : noOfDays = 30;
				break;
				case 6 : noOfDays = 31;
				break;
				case 7 : noOfDays = 30;
				break;
				case 8 : noOfDays = 31;
				break;
				case 9 : noOfDays = 30;
				break;
				case 10 : noOfDays = 31;
				break;
				case 11 : noOfDays = 31;
				break;
				default : noOfDays = 30;
				break;
			}
			trace(this, ' : daysInCurrentMonth : month=', monthNum, ' : days=', noOfDays);
			//
			return noOfDays;
		}
		
		
		
		
	}

}